<?php
//maked smaller tiled tiffs from a larger raster geotiff.
//created by brandon donnelson 8/1/06
//geotiff tiler php5 script


//run php5 ./tile_tiff.php


$BaseLeft 	= "/srv/hosting_files/maps/aerial/snohomish_king/2003";
$BaseRight 	= "/srv/hosting_files/maps/aerial/snohomish_king/2003/tiled";
$NewTiffName 	= "geotiff.tif";
$NewTiffSize 	= 1000; //pixels wide

//get geotiff files in a directory
//@BaseLeft - Base Directory - Where are the files that we will work with
//@BaseRight - Base Directory - Where will we put the new tiled tiffs 
//@NewTiffName - What will we name the new geotiff tilled images
function fun_GeoTiff_GetFiles($BaseLeft, $BaseRight, $NewTiffName, $NewTiffSize)
{
	//make dir if doesn't exist -> this is the new directory location that the new tiled tiffs go
	if (!is_dir($BaseRight))
	{
		mkdir($BaseRight);
	}

	$dh  = opendir($BaseLeft);
	while (false !== ($filename = readdir($dh))) 
	{
		$files[] = $filename;
	}
	
	//debug
	//print_r($files);

	sort($files);
	
	//move through files in the directory and process the tiffs to make new tiled tiffs
	$i=0;
	foreach ($files as $value)
	{
		$LeftFile = $BaseLeft . "/" . $value; //made for linux, change to "\\" for windows


		//working with a file?
  		if (is_file($LeftFile) and preg_match("/tif/i", $LeftFile))
		{
			$RightFile = $BaseRight . "/" . $i . "_" . $NewTiffName;  //ie. /srv/dir/0_geotiff.tif

			echo "Working On GeoTiff: Left:$LeftFile Right:$RightFile\n";

			fun_GeoTiff_MakeTiles($LeftFile, $RightFile, $NewTiffSize);
		}//end check if file
		$i++;
	}//end foreach

}//end fun_GeoTiff_GetFiles


//tile a larger geotiff to tiled tiffs
//@LeftFile - geotiff file we want to tile
//@RightFile - File name we are going to convert the new geotiff file into
//@NewTiffSize - This is the new GeoTiffSize. ->!!!! it better work out to evenly divide the tiff image!!!!!!! 
// like 6000 pixels wide square, you would do 1000.
function fun_GeoTiff_MakeTiles($LeftFile, $RightFile, $NewTiffSize)
{	
	//left - orginal tiff
	//right - smaller tiff
	
	//$LeftGeoTiff  = "/srv/hosting_files/maps/aerial/snohomish_king/2003/test/12484320.tif";
	$LeftGeoTiff = $LeftFile;
	//$RightGeoTiff = "/srv/hosting_files/maps/aerial/snohomish_king/2003/test/NewTiff.tiff";
	$RightGeoTiff = $RightFile;
	
	//setup tile parameters
	
	//figure out the size of the image -gd lib
	$imagedata = GetImageSize($LeftGeoTiff); 
	$width 	= $imagedata[0]; 
	$height = $imagedata[1]; 
	
	
	
	//debug
	//print_r($imagedata);

	
	
	//how many tiles across and down???
	$HorzTiles = $width / $NewTiffSize; // = 6
	$VertTiles = $height / $NewTiffSize; // = 6
	
	//going down the image
	for ($i=0; $i < $VertTiles; $i++)
	{
	
		//across the image
		for ($i2=0; $i2 < $HorzTiles; $i2++)
		{
			//setup offsets
			if ($i==0)
			{
				$XoffSet = 0; //not to divide by 0
			}
			else
			{
				$XoffSet = $NewTiffSize * $i;
			}
	
			if ($i2==0)
			{
				$Yoffset = 0; 
			}
			else
			{
				$Yoffset = $NewTiffSize * $i2;
			}
	
	
			$AddToFile = "{$i}_{$i2}";
	
			//setup new image name
			$RightGeoTiffNew = ereg_replace("\.[a-zA-Z]+", "_$AddToFile.tif", $RightGeoTiff);
	
			echo "Tile x$i y$i2:";
	
			//ie. gdal_translate -srcwin 0 0 2000 2000 in.tif out_0_0.tif
			//ie. gdal_translate -srcwin 2000 0 2000 2000 in.tif out_1_0.tif
			$cmd = "gdal_translate -srcwin $XoffSet $Yoffset $NewTiffSize $NewTiffSize $LeftGeoTiff $RightGeoTiffNew";
	
			echo "$cmd\n";
			
			//execute the shell command
			exec($cmd);
	
		}//end i2
	
	}//end i
}//end fun_GeoTiff_MakeTiles


fun_GeoTiff_GetFiles($BaseLeft, $BaseRight, $NewTiffName, $NewTiffSize);
?>